<template>
	<view class="knowledge-list" :class="[config.style]">
		<!-- 顶部固定部分 -->
		<view class="top-warp">
			<!--顶部导航菜单-->
			<view class="top-nav" v-if="userInfo.sessionid">
				<view class="item histroy" @click="goToHistory">
					<view class="title">足迹</view>
					<view>{{historyTotal}}</view>
				</view>
				<view class="item favorites" @click="goToFavorites">
					<view class="title">收藏</view>
					<view>{{favoritesTotal}}</view>
				</view>
				<view class="item orders" @click="goToOrders">
					<view class="title">订单</view>
					<view>{{ordersTotal}}</view>
				</view>
			</view>
			<view class="tabs-block">
				<view class="tabs">
					<ListTabs v-model="tabIndex" :tabs="tabs" @change="tabChange"></ListTabs>
				</view>
				
				<view class="attribute" @click="showDrawer('showRight')">
					<image src="../../../static/images/attribute.png" mode="widthFix" class="image"></image>
					<text class="text">筛选</text>
				</view>
				
			</view>
		</view>
		<!--筛选抽屉部分-->
		<view>
			<block v-if="loadAttribute">
			<Attribute ref="attributeDrawer" page="knowledge" :urlParams="urlParams" :config="config"></Attribute>
			</block>
		</view>
		<!--数据列表部分-->
		<view class="list-section uni-clearfix">
			<!-- top="xxx"下拉布局往下偏移,防止被悬浮菜单遮住 -->
			<mescroll-uni ref="mescrollRef" @init="mescrollInit" :top="mescrollTop" :down="downOption" @down="downCallback" @up="upCallback">
				<!-- 数据列表 -->
				<view class="list rank0">
					<view class="item" v-for="(item,index) in goods" :key="index" @click="goToDetailLink(item.id)">
						<view class="content">
							<view class="cover">
								<image mode="widthFix" :src="item.cover_200" @load="imageLoad" class="image"></image>
							</view>
							<view class="info">
								<view class="title text-ellipsis-2">{{item.title}}</view>
								<view class="price">￥ {{item.price == 0 ? '免费':item.price}}</view>
								<view class="exp-info">
									<view class="item view" v-if="config.config.show.view == 1">
										<uni-icons type="eye" size="10" color="#999"></uni-icons> 
										<text class="num">{{item.handling_view}}</text>
									</view>
									<view class="item sales" v-if="config.config.show.sales == 1">
										<uni-icons type="cart-filled" size="10" color="#999"></uni-icons> 
										<text class="num">{{item.handling_sales}}</text>
									</view>
									<view class="item favorites" v-if="config.config.show.favorites == 1">
										<uni-icons type="star" size="10" color="#999"></uni-icons> 
										<text class="num">{{item.handling_favorites}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</mescroll-uni>
		</view>
		<!--登录框-->
		<Login ref="login" :config="config" @getUserInfo="getUserInfo"/>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import ListTabs from "../common/listTabs"; //列表tab
	import Attribute from "../common/minishopAttribute"; //筛选抽屉

	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			ListTabs,
			Attribute,
		},
		data() {
			return {
				config : {}, //全局配置
				urlParams: {}, //url传递过来的参数
				loadAttribute: false,
				page: 1, //当前页码
				goods: [], //列表数据
				tabs: ['默认', '浏览量', '热卖', '价格'],
				tabIndex: 0 ,// tab下标
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption:{
					// page: {
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 搜索无数据 ~', // 提示
						btnText: '去看看'
					}
				},
				mescrollTop: 158,
				userInfo: {},
				ordersTotal: 0, //小店订单数量
				favoritesTotal: 0, //小店收藏数量
				historyTotal: 0, //小店浏览记录数量
			}
		},
		onLoad(option) {
			//获取url参数
			this.getUrlParam()
			this.getConfig()
			//获取已登录用户数据
			this.getUserInfo()
		},
		created() {
			
		},
		methods: {
			/**
			 * 获取配置
			 */
			getConfig: function(){
				let that = this
				const config = uni.getStorageSync('minishop_config');
				if(config){
					that.config = config
				}else{
					//获取全局配置
					this.util.request({
						url : 'entry/wxapp/minishop',
						data: {
							'action' : 'config'
						},
						success: function (res) {
							//console.log(res.data);
							if (res.data.code == 200) {
								console.log('config')
								that.config = res.data.data
								//设置原生导航栏
								that.setNavigation(that.config.style);
								uni.setNavigationBarTitle({
								　　title: that.config.title
								})
							}
						}
					})
				}				
			},
			setNavigation: function(style) {
				let that = this
				//动态更改顶部状态栏演示
				let backgroundColor = '#ffffff'
				//console.log(this.config)
				if(style == 'Green'){
					backgroundColor = '#00ce74';
				}
				if(style == 'Blue'){
					backgroundColor = '#03b8cf';
				}
				if(style == 'LightRed'){
					backgroundColor = '#ea644a';
				}
				if(style == 'Orange'){
					backgroundColor = '#ff9900';
				}
				if(style == 'LightPink'){
					backgroundColor = '#ffb6c1';
				}
				if(style == 'Magenta'){
					backgroundColor = '#8666b8';
				}
				console.log(backgroundColor);
				//更改小程序原生导航栏
				uni.setNavigationBarColor({
					frontColor: '#ffffff',
					backgroundColor: backgroundColor,
				})
			},
			//获取url参数
			getUrlParam: function(){
				// #ifdef MP-WEIXIN
				//console.log('小程序端执行')
				let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let curRoute = routes[routes.length - 1].route //获取当前页面路由
				let curParam = routes[routes.length - 1].options; //获取路由参数
				// #endif
			
				// #ifdef H5
				let curParam = this.$route.query;
				// #endif
				this.urlParams = curParam
				this.loadAttribute = true
				//console.log(this.urlParams)
			},
			// 打开筛选抽屉
			showDrawer(e) {
				//触发子组件内方法
				this.$refs.attributeDrawer.$refs[e].open()
			},
			imageLoad: function(e) {
				//console.log(e.detail)
			},
			//上拉加载的回调
			upCallback: function() {
				let that = this
				let keyword = this.tabs[this.tabIndex]
				console.log(keyword)
				let data = this.urlParams
					data.action = 'lists'
					data.m = 'muu_classroom'
					data.page = this.page
				//获取首页数据
				this.util.request({
					url : 'entry/wxapp/minishop',
					data : data,
					success: function (res) {
						//console.log(res.data.code);
						if (res.data.code == 200) {
							let goods = res.data.data.data
							that.goods = that.goods.concat(goods);
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(goods.length);
							that.page = that.page + 1
							//console.log(that.goods.length)
						}
					}
				})
			},
			//下拉刷新
			downCallback: function() {
				let that = this
				let keyword = this.tabs[this.tabIndex]
				console.log(keyword)
				this.page = 1
				
				let data = this.urlParams
					data.action = 'lists'
					data.page = this.page
				//获取分页列表首页数据
				this.util.request({
					url : 'entry/wxapp/minishop',
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.goods = []
							that.goods = res.data.data.data
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(that.goods.length);
							that.page = 2
						}
					}
				})
			},
			//点击空布局按钮的回调
			emptyClick(){
				uni.showToast({
					title:'点击了按钮,具体逻辑自行实现'
				})
			},
			// 切换TAB
			tabChange() {
				this.page = 1
				this.goods = []// 先置空列表,显示加载进度
				this.mescroll.resetUpScroll() // 再刷新列表数据
			},
			
			//路由至详情页
			goToDetailLink: function(id){
				let urlParams = 'id='+id
				let url = 'detail?' + urlParams
				//console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			},
			/**
			 * 进入小店浏览记录页
			 */
			goToHistory: function(){
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {};
				if (userInfo.sessionid) {
					let url = 'history'
					uni.navigateTo({
						url: url
					});
				}else{
					//触发登录框
					that.$refs.login.loginDialog()
				}
			},
			/**
			 * 获取浏览记录数
			 */
			getHistoryTotal: function(){
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {};
				if (userInfo.sessionid) {
					this.util.request({
						url : 'entry/wxapp/minishop',
						data : {
							action: 'history',
							target: 'count',
						},
						success: function (res) {
							if (res.data.code == 200) {
								//console.log(res.data)
								that.historyTotal = res.data.data
							}
						}
					})
				}
			},
			/**
			 * 进入小店收藏列表页
			 */
			goToFavorites: function(){
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {};
				if (userInfo.sessionid) {
					let url = 'favorites'
					uni.navigateTo({
						url: url
					});
				}else{
					//触发登录框
					that.$refs.login.loginDialog()
				}
			},
			/**
			 * 获取浏览记录数
			 */
			getFavoritesTotal: function(){
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {};
				if (userInfo.sessionid) {
					this.util.request({
						url : 'entry/wxapp/minishop',
						data : {
							action: 'favorites',
							target: 'count',
						},
						success: function (res) {
							if (res.data.code == 200) {
								//console.log(res.data)
								that.favoritesTotal = res.data.data
							}
						}
					})
				}
			},
			/**
			 * 进入小店我的订单
			 */
			goToOrders: function(){
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {};
				if (userInfo.sessionid) {
					let url = 'orders'
					uni.navigateTo({
						url: url
					});
				}else{
					//触发登录框
					that.$refs.login.loginDialog()
				}
			},
			/**
			 * 获取订单记录数
			 */
			getOrdersTotal: function(){
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {};
				if (userInfo.sessionid) {
					this.util.request({
						url : 'entry/wxapp/minishop',
						data : {
							action: 'orders',
							target: 'count',
						},
						success: function (res) {
							if (res.data.code == 200) {
								//console.log(res.data)
								that.ordersTotal = res.data.data
							}
						}
					})
				}
			},
			/**
			 * 获取已登录用户数据
			 */
			getUserInfo: function(){
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {}
				this.userInfo = userInfo
				console.log(userInfo)
				if (userInfo.sessionid) {
					this.mescrollTop = 158
					this.getHistoryTotal()
					this.getFavoritesTotal()
					this.getOrdersTotal()
				}else{
					this.mescrollTop = 68
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #f8f8f8;
		min-height: 100%;
		height: auto;
	}
	.top-nav {
		display: flex;
		padding: 10rpx;
		.item {
			flex: 1;
			text-align: center;
			font-size: 28rpx;
			color: #fff;
			.title {
				font-size: 26rpx;
			}
		}
	}
	/*顶部固定部分*/
	.top-warp {
		background-color: #ffffff;
		.tabs-block {
			display: flex;
			flex-wrap: wrap;
			margin: 0 20rpx;
			.tabs {
				flex: 1;
			}
			.attribute {
				width: 120rpx;
				text-align: center;
				padding-top: 17rpx;
				.image {
					display: inline-block;
					width: 32rpx;
					height: 32rpx;
					margin-right: 10rpx;
					vertical-align:top;
				}
				.text {
					display: inline-block;
					font-size: 24rpx;
					color: #666;
					vertical-align:top;
				}
			}
		}
	}

	/*横排*/
	.list.rank0 {
		/*小图*/
		display: flex;
		flex-wrap: wrap;
		>.item {
			box-sizing:border-box;
			width: calc(50% - 20rpx);
			background-color: #fff;
			margin: 10rpx;
			border-radius: 10rpx;
			box-shadow: 0 0 10rpx rgba(0,0,0,.02);
			overflow: hidden;
			.content {
				.cover {
					position: relative;
					overflow: hidden;
					.image {
						border-radius: 10rpx 10rpx 0 0;
						width: 100%;
						height: auto;
						display: block;
					}
				}
				.info {
					padding: 10rpx;
					.title {
						font-size: 28rpx;
						color: #000;
						font-weight: 600;
						height: 72rpx;
						line-height: 36rpx;
					}
					.description {
						color: #999;
						font-size: 24rpx;
					}
					.price {
						margin-top: 10rpx;
						font-size: 24rpx;
					}
					.exp-info {
						margin-top: 10rpx;
						color: #999;
						font-size: 24rpx;
						.item {
							display: inline;
							margin: 0;
							.num {
								margin: 0 10rpx;
							}
						}
					}
				}
			}
		}
	}
	/*竖排*/
	.list.rank1 {
		/*小图*/
		display: flex;
		flex-wrap: wrap;
		.item {
			width: 100%;
			.content {
				padding: 10rpx;
				display: flex;
				flex-wrap: wrap;
				.cover {
					position: relative;
					width: 280rpx;
					border-radius: 10rpx;
					overflow: hidden;
					.image {
						width: 100%;
						height: auto;
						display: block;
					}
				}
				.info {
					padding-left: 20rpx;
					width: calc(100% - 300rpx);
					.title {
						font-size: 28rpx;
						color: #000;
						font-weight: 600;
						height: 72rpx;
						line-height: 36rpx;
					}
					.description {
						color: #999;
						font-size: 24rpx;
					}
					.price {
						margin-top: 10rpx;
						font-size: 24rpx;
					}
					.exp-info {
						margin-top: 10rpx;
						color: #999;
						font-size: 24rpx;
						.item {
							display: inline;
							.num {
								margin: 0 10rpx;
							}
						}
					}
				}
			}
		}
	}
	
	/*页面风格设计*/
	.Blue {
		.top-nav {
			background-color: $Blue;
		}
		.price {
			color: $Blue;
		}
	}
	.Green {
		.top-nav {
			background-color: $Green;
		}
		.price {
			color: $Green;
		}
	}
	.LightRed {
		.top-nav {
			background-color: $LightRed;
		}
		.price {
			color: $LightRed;
		}
	}
	.Orange {
		.top-nav {
			background-color: $Orange;
		}
		.price {
			color: $Orange;
		}
	}
	.LightPink {
		.top-nav {
			background-color: $LightPink;
		}
		.price {
			color: $LightPink;
		}
	}
	.Magenta {
		.top-nav {
			background-color: $Magenta;
		}
		.price {
			color: $Magenta;
		}
	}
</style>
